﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;
using System.Windows.Forms;

namespace YTSK.cSK.BW
{
    class cKP_DZP
    {

        [System.Runtime.InteropServices.ComVisibleAttribute(true)]
        public static string proc(string filepath,  out string fpdm, out string fphm)
        {
            var err = "";
            fphm = "";
            fpdm = "";

            var sname = new StringBuilder();
            #region 获取主窗口
            sname.Append("增值税电子普通发票");
            var mainwin = new Model.WinProperty();
            mainwin.hwnd = cDll.FindWindow(null, sname);

            //if (mainwin.hwnd.ToInt32() == 0)
            //{
            //    sname = new StringBuilder();
            //    cMainWindowName = "广西增值税电子发票开具";
            //    sname.Append(cMainWindowName);
            //    mainwin = new Model.WinProperty();
            //    mainwin.hwnd = cDll.FindWindow(null, sname);
            //}
            //if (mainwin.hwnd.ToInt32() == 0)
            //{
            //    sname = new StringBuilder();
            //    cMainWindowName = "深圳增值税电子发票开具";
            //    sname.Append(cMainWindowName);
            //    mainwin = new Model.WinProperty();
            //    mainwin.hwnd = cDll.FindWindow(null, sname);
            //}

            if (mainwin.hwnd.ToInt32() == 0)
            { err = "没有找到开票系统的窗口，请先打开"; return err; }
            cDllExt.GetWinProperty(mainwin);
            cDll.SwitchToThisWindow(mainwin.hwnd, true);
            #endregion


            #region 获取发票号码
            err = GetVouchCode(out fpdm, out fphm);
            if (err.Length>0) { return err; }
            #endregion

            #region 点击导入按钮
            //cDll.SetForegroundWindow(mainwin.hwnd);
            //cDll.SetCursorPos(mainwin.position.Left, mainwin.position.Top);
            int x = mainwin.position.Left + 312 + 5;
            int y = mainwin.position.Top + 40 + 5;
            cDllExt.mouseClick(x, y);
            #endregion
            System.Threading.Thread.Sleep(500);
            #region 点击手工导入
            x = mainwin.position.Left + 312 + 5;
            y = mainwin.position.Top + 40 + 85;
            cDllExt.mouseClick(x, y);
            #endregion

            #region 选择导入文件
            System.Threading.Thread.Sleep(500);
            var openwin = new Model.WinProperty();
            sname = new StringBuilder();
            sname.Append("打开");
            //openwin.hwnd = cDll.FindWindowEx(mainwin.hwnd, IntPtr.Zero, null, openname);
            openwin.hwnd = cDll.FindWindow(null, sname);
            cDllExt.GetWinProperty(openwin);
            var openmainOle = AutomationElement.FromHandle(openwin.hwnd);
            var condition0 = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit);
            var condition1 = new PropertyCondition(AutomationElement.NameProperty, "文件名(N):");
            var andcondition = new AndCondition(condition0, condition1);
            var eliemt = openmainOle.FindFirst(TreeScope.Descendants, andcondition);
            var vt = (ValuePattern)eliemt.GetCurrentPattern(ValuePattern.Pattern);
            vt.SetValue(filepath);
            condition0 = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button);
            condition1 = new PropertyCondition(AutomationElement.NameProperty, "打开(O)");
            andcondition = new AndCondition(condition0, condition1);
            eliemt = openmainOle.FindFirst(TreeScope.Children, andcondition);
            var clickpattern = (InvokePattern)eliemt.GetCurrentPattern(InvokePattern.Pattern);
            clickpattern.Invoke();
            //MessageBox.Show(openwin.text.ToString());

            #endregion
            System.Threading.Thread.Sleep(1000);
            #region 导入单据选择窗体
            sname = new StringBuilder();
            sname.Append("导入单据选择");
            var importwin = new Model.WinProperty();
            importwin.hwnd = cDll.FindWindow(null, sname);
            if (importwin.hwnd.ToInt32() == 0)
            { err = "没有找到导入确认窗口"; return err; }
            cDllExt.GetWinProperty(importwin);
            cDll.SetCursorPos(importwin.position.Left + 107 + 5, importwin.position.Top + 40 + 5);
            cDll.mouse_event(MouseEvent.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
            cDll.mouse_event(MouseEvent.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
            #endregion
            return err;

        }

        private List<Model.SKFP> GetMoney(Model.WinProperty main, int irows)
        {
            var l = new List<Model.SKFP>();
            var lw = cDllExt.GetAllChild(main.hwnd);
            var grid = lw.FirstOrDefault(s => s.position.Left == main.position.Left + 11
              & s.position.Top == main.position.Top + 244);
            if (grid == null) { return l; }
            for (int i = 0; i < irows; i++)
            {
                var skfp = new Model.SKFP();
                var top = grid.position.Top + i * 16 + 19 + 5;

                #region 存货名称--左边长度14
                var left = grid.position.Left + 14 + 15;
                string stext;
                var bret = GetGridData(grid, ref left, top, out stext);
                if (!bret) { return l; }
                skfp.invname = stext;
                #endregion

                #region 规格型号
                bret = GetGridData(grid, ref left, top, out stext);
                if (!bret) { return l; }
                skfp.invstd = stext;
                #endregion

                #region 单位
                bret = GetGridData(grid, ref left, top, out stext);
                if (!bret) { return l; }
                skfp.dw = stext;
                #endregion

                #region 数量
                bret = GetGridData(grid, ref left, top, out stext);
                if (!bret) { return l; }
                skfp.quantity = decimal.Parse(stext);
                #endregion

                #region 单价
                bret = GetGridData(grid, ref left, top, out stext);
                if (!bret) { return l; }
                skfp.unitprice = decimal.Parse(stext);
                #endregion

                #region 金额
                bret = GetGridData(grid, ref left, top, out stext);
                if (!bret) { return l; }
                skfp.totalprice = decimal.Parse(stext);
                #endregion

                #region 税率
                bret = GetGridData(grid, ref left, top, out stext);
                if (!bret) { return l; }
                skfp.taxrate = decimal.Parse(stext);
                #endregion

                #region 税额
                bret = GetGridData(grid, ref left, top, out stext);
                if (!bret) { return l; }
                skfp.tax = decimal.Parse(stext);
                #endregion
                l.Add(skfp);
            }

            //var grid = AutomationElement.FromHandle(gridori.hwnd);


            return l;
        }

        private bool GetGridData(Model.WinProperty grid, ref int x, int y, out string text)
        {
            cDllExt.mouseDoubleClick(x, y);
            var lw = cDllExt.GetAllChild(grid.hwnd);
            cDll.keybd_event((byte)Keys.Escape, 0, 0, 0);
            cDll.keybd_event((byte)Keys.Escape, 0, 2, 0);
            if (lw.Count > 0)
            {
                var a = lw[0];
                x = a.position.Width + 10;
                text = a.text; return true;
            }
            else { text = ""; return false; }
        }

        [System.Runtime.InteropServices.ComVisibleAttribute(true)]
        private static string GetVouchCode(out string fpdm, out string fphm)
        {
            var err  = ""; fpdm = ""; fphm = "";
            var sname = new StringBuilder();
            sname.Append("发票确认");
            var mainwin = new Model.WinProperty();
            mainwin.hwnd = cDll.FindWindow(null, sname);
            if (mainwin.hwnd.ToInt32() == 0) { err = "没有找到发票窗口"; return err; }
            cDllExt.GetWinProperty(mainwin);
            var childrens = cDllExt.GetAllChild(mainwin.hwnd);
            var tmps = childrens.Where(s => Math.Abs(s.position.Left - mainwin.position.Left - 223) < 5
              & Math.Abs(s.position.Top - mainwin.position.Top - 208) < 5).ToList();
            if (tmps.Count > 0)
            { fpdm = tmps[0].text.ToString(); }
            else { err = "没有找到发票代码"; return err; }
            tmps = childrens.Where(s => Math.Abs(s.position.Left - mainwin.position.Left - 223) < 5
              & Math.Abs(s.position.Top - mainwin.position.Top - 231) < 5).ToList();
            if (tmps.Count > 0)
            { fphm = tmps[0].text.ToString(); }
            else { err = "没有找到发票号码"; return err; }
            return err;
        }




    }
}
